package com.project.finance.mobile.exchange.controller;

import com.project.finance.common.po.TbExchange;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;

import com.project.finance.mobile.exchange.service.MobileExchangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController

public class MobileExchangeController {
    @Autowired
    private MobileExchangeService exchangeService;
//查汇率
   @RequestMapping(value = "exchangeList",method = RequestMethod.GET)
    public Result exchangeList(){

       List<TbExchange> tbExchanges= exchangeService.listExchange();
//对数据进行组装
       HashMap<Object, Object> resultMap = new HashMap<>();
       ArrayList<HashMap<String, Object>> resultList = new ArrayList<>();

       for (TbExchange tbExchange : tbExchanges) {
           HashMap<String, Object> map = new HashMap<>();
           map.put("exchangeId",tbExchange.getExchangeId());
           map.put("countryName",tbExchange.getCountryName());
           map.put("exRate",tbExchange.getExRate());
           resultList.add(map);
       }
       resultMap.put("exchangeList",resultList);
       return ResultGenerator.apiSuccess(resultMap);
    }
    //汇率计算
    @RequestMapping(value = "exchange",method = RequestMethod.GET)
    public Result selectRate(@RequestParam("money") int money,
                             @RequestParam("exchangeId1")Integer exchangeId1,
                             @RequestParam("exchangeId2") Integer exchangeId2){
        BigDecimal v1 = exchangeService.selecteRate(exchangeId1);
        BigDecimal v2 = exchangeService.selecteRate(exchangeId2);
        double c1 = v1.doubleValue();
        double c2 = v2.doubleValue();
        double resultMoney= money / c1 * c2;
        return ResultGenerator.apiSuccess(resultMoney);
    }


}
